<?php 
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4496 2010-08-12 01:58:49Z huuphuoc $
 * @since		2.0.0
 */
?>
<?php 
$this->headTitle()->prepend($this->translate('role_list_page_title').' - ');
$this->headScript()
      ->appendFile($this->APP_STATIC_SERVER.'/public/js/jquery.json-2.3.min.js')
	->appendFile($this->APP_STATIC_SERVER . '/public/js/jquery.validate.js');
?>
<div id="r-heading">
	<img src="<?php echo $this->APP_STATIC_SERVER.'/skins/admin/default/images/role-48x48.png';?>" alt=""/>
	<span><?php echo $this->translate('role_list_page_title') ?></span>
</div>
<?php
$message = $this->flashMessenger();
if($message){
?>
<div id ="message"><?php echo $message ?></div>
<?php
}
?>
<div class="guide"><?php echo $this->translate('role_list_guide'); ?></div>

<div class="r-left" style="width: 60%; float: left;">
    <table class="list" cellspacing="0">
        <thead>
            <tr>
                <td><?php echo $this->translate('role_list_name'); ?></td>
                <td><?php echo $this->translate('role_list_desc'); ?></td>
                <td><?php echo $this->translate('role_list_status'); ?></td>
                <td><?php echo $this->translate('role_list_num_user'); ?></td>
                <td><?php echo $this->translate('role_list_actions'); ?></td>
            </tr>
        </thead>
        <tbody>
        <?php
            $count = count($this->roles);
            if($count > 0){
                $i = 0;
                foreach($this->roles as $role){
        ?>
                    <tr id="role_<?php echo $role->id ?>" <?php if ($i == $count - 1) echo 'class="last"'; ?>>
                        <td><?php echo $role->name ?></td>
                        <td><?php echo $role->description  ?></td>
                        <td class="role-lock">
                                <?php if ($role->locked)
                                        echo $this->translate('role_list_locked');
                                else
                                    echo $this->translate('role_list_unlocked'); ?>
                        </td>
                        <td><?php echo $role->num_users ?></td>
                        <td>
                            <?php if ($this->allow('lock')) : ?>
				<a class="lockAction" rel="<?php echo $role->id .'_'.$role->locked; ?>" href="javascript: void(0);"><?php if ($role->locked) : ?><?php echo $this->translate('role_list_action_unlock'); ?><?php else : ?><?php echo $this->translate('role_list_action_lock'); ?><?php endif; ?></a>
				<?php endif; ?>
                        <?php if ($this->allow('delete')) : ?>
				<?php $displayed = ($role->num_users || $role->locked) ? ' style ="display: none;"' : ''?>
                        | <a <?php echo $displayed ?> class="deleteAction" rel="<?php echo $role->id; ?>_<?php echo $role->name; ?>" href="javascript: void(0);"><?php echo $this->translate('role_list_action_delete'); ?></a>
				<?php endif; ?>
                        <?php if ($this->routeAccessor('core_rule_set_role')) : ?>
				| <a href="<?php echo $this->url(array('id' => $role->id), 'core_rule_set_role'); ?>"><?php echo $this->translate('role_list_action_rule'); ?></a>
				<?php endif; ?>
                        </td>
        <?php
                }
            }else{
        ?>
            <tr>
                <td colspan="7" style="color: #fc0202;">
                <?php echo $this->translate('role_list_not_found'); ?>
                </td>
            </tr>
        <?php
            }
        ?>
        </tbody>
        <tfoot>
            <tr>
                <td><?php echo $this->translate('role_list_name'); ?></td>
                <td><?php echo $this->translate('role_list_desc'); ?></td>
                <td><?php echo $this->translate('role_list_status'); ?></td>
                <td><?php echo $this->translate('role_list_num_user'); ?></td>
                <td><?php echo $this->translate('role_list_actions'); ?></td>
            </tr>
        </tfoot>
    </table>
</div>
<div class="r-right" style="width: 35%; float: right;">
<?php if($this->allow('add') || ($this->allow('edit') && isset($this->role))){
    if(!isset($this->role)){
?>
    <div class="box">
        <div class="b-title"><?php echo $this->translate('role_list_add_head'); ?></div>
        <div class="b-container">
            <form method="post" id="frmRole" name="frmRole" action="<?php echo $this->url(array(), 'core_role_add'); ?>">
                <div  class="row">
                    <label><?php echo $this->translate('role_list_name'); ?>:</label>
                    <input type="text" size="50" name="name" id="name" />
                </div>
                <div class="row">
                      <label><?php echo $this->translate('role_list_desc'); ?>:</label>
                      <input type="text" size="50" name="description" id="description" />
                </div>
                <div >
                      <input type="checkbox" name="lock" value="1" /><label> <?php echo $this->translate('role_list_lock'); ?></label>
                </div>
                <div>
                      <button class="button" type="submit"><span><?php echo $this->translate('role_list_add_button'); ?></span></button>
                </div>
            </form>
        </div>
    </div>
<?php
    }else{
?>
    <div class="box">
        <div class="b-title"><?php echo $this->translate('role_list_edit_head'); ?></div>
        <div class="b-container">
            <form method="post" id="frmRole" name="frmRole" action="<?php echo $this->url(array(), 'core_role_role'); ?>">
                <div class="row">
                    <label ><?php echo $this->translate('role_list_name'); ?></label>
                    <input type="text" size="50" name="name" id="name" value="<?php echo $this->role->name?>"/>
                </div>
                <div class="row">
                      <label><?php echo $this->translate('role_list_desc'); ?></label>
                      <input type="text" size="50" name="description" id="description" value="<?php echo $this->role->description ?>"/>
                </div>
                <div >
                      <input type="checkbox" name="lock" value="1" <?php echo (($this->role->locked) ? 'checked="checked"' : '')?>/>
                      <label><?php echo $this->translate('role_list_lock'); ?></label>
                </div>
                <div>
                      <button class="button" type="submit"><span><?php echo $this->translate('role_list_edit_button'); ?></span></button>
                </div>
            </form>
        </div>
    </div>
<?php
    }
}
?>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        <?php if ($this->allow('lock')) : ?>
        $('a.lockAction').click(function() {
		var self = this;
		var rels = $(this).attr('rel').split('_');
		$(this).parent().parent().find(".role-lock").html("<img src='<?php echo $this->APP_STATIC_SERVER.'/skins/admin/default/images/loading.gif'?>' alt='' />");
		$.ajax({
                url: '<?php echo $this->url(array(), 'core_role_lock'); ?>',
                type: 'POST',
                data: { id: rels[0], lock: rels[1] },
                success: function(response) {

                    if(response == 'RESULT_OK'){
                        rels[1] = 1 - rels[1];
                        $(self).attr('rel', rels[0] + '_' + rels[1]);
                    }
                    if (rels[1] == 1) {
                        $(self).next().css("display", "none");
                        $(self).html("<?php echo $this->translate('role_list_action_unlock'); ?>");
                        $(self).parent().parent().find(".role-lock").html("<?php echo $this->translate('role_list_locked'); ?>");
                    } else {
                        $(self).next().css("display", "");
                        $(self).html("<?php echo $this->translate('role_list_action_lock'); ?>");
                        $(self).parent().parent().find(".role-lock").html("<?php echo $this->translate('role_list_unlocked'); ?>");
                    }
                }
		});
//            var str = rels[0] + '_' + rels[1];
//            alert(str);
//            $(this).attr('rel',str);
	});
	<?php endif; ?>
	
	<?php if ($this->allow('delete')) : ?>
	$('a.deleteAction').click(function() {
		var self = this;
            var rels = $(this).attr('rel').split('_');
            if(confirm("<?php echo $this->translate('role_list_delete_confirm'); ?>")){
                $.ajax({
                      url: '<?php echo $this->url(array(), 'core_role_delete'); ?>',
                      type: 'POST',
                      data: { id: rels[0] },
                      success: function(response) {
                          if(response == "RESULT_OK"){
                            $('#role_' + rels[0]).fadeOut('slow');
                          }
                      }
                });
            }
		
	});	
	<?php endif; ?>

	/**
	 * Validate form
	 */
	$('#frmRole').validate({
		rules: {
			name: { 
				required: true 
			}
	    },
	    messages: {
	    	name: {
	        	required: "<?php echo $this->translate('role_list_name_required'); ?>"
			}
		}
	});
});
</script>
